package com.example.excel.servlet;

import com.example.excel.entity.Student;
import com.example.excel.service.StudentService;
import com.example.excel.util.CaptchaUtil;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LoginServlet", urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    private final StudentService Service = new StudentService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        //获取前端用户输入数据
        String name = req.getParameter("username");
        String pwd = req.getParameter("password");
        String captcha = req.getParameter("captchaInput");

        if (!CaptchaUtil.verify(captcha, req)) {
            //将用户输入的用户名及密码保留
            req.setAttribute("name", name);
            req.setAttribute("password", pwd);
            System.out.println("验证码错误");
            req.setAttribute("Error",0);//写入错误码调用前端弹窗函数
            //返回登陆界面
            req.getRequestDispatcher("index.jsp").forward(req, resp);

            return;  // 结束方法的执行
        }
        //用户名非空判断
        if (name == null || name.isEmpty()) {
            System.out.println("用户名为空");
            //返回登陆界面
            req.getRequestDispatcher("index.jsp").forward(req, resp);
            return;  // 结束方法的执行
        } else if (pwd == null || pwd.isEmpty()) {
            System.out.println("密码为空");
            //返回登陆界面
            req.getRequestDispatcher("index.jsp").forward(req, resp);
            return;  // 结束方法的执行
        }

        //进行数据库比对
        if (Service.countForName(name) == 0) {
            System.out.println("用户名错误");
            req.setAttribute("password", pwd);//保留密码
            req.setAttribute("Error",1);//写入错误码调用前端弹窗函数
            //返回登陆界面
            req.getRequestDispatcher("index.jsp").forward(req, resp);
            return;  // 结束方法的执行
        } else if (Service.countForPwd(pwd) == 0) {
            System.out.println("密码错误");
            req.setAttribute("name", name);//保留用户名
            req.setAttribute("Error",2);//写入错误码调用前端弹窗函数
            //返回登陆界面
            req.getRequestDispatcher("index.jsp").forward(req, resp);
            return;  // 结束方法的执行
        } else if (Service.countForLogin(name, pwd) > 0) {
            System.out.println("登录成功");

            Student asd = Service.oneStudent(name, pwd);
            req.setAttribute("student",asd);//获取用户对象写入请求头
            req.setAttribute("Students",Service.findAll());//获取全部用户对象写入请求头
            HttpSession s = req.getSession();//获取session对象
            s.setAttribute("login",true);//设置登录成功后在session中更改属性以便监听器监听
            s.setMaxInactiveInterval(10 * 60);//设置时间十分钟不活动自动销毁session,清除在线状态
            req.getRequestDispatcher("excelShow.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
